//
//  ViewController.swift
//  DynamicsDemo
//
//  Created by cygarm on 15/8/17.
//  Copyright (c) 2015年 cygarm. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UICollisionBehaviorDelegate {
	var animator: UIDynamicAnimator!
	var gravity: UIGravityBehavior!
	var collision: UICollisionBehavior!
	var firstContact = false
	var square: UIView!
	var snap: UISnapBehavior!
	
	override func viewDidLoad() {
		super.viewDidLoad()
		// Do any additional setup after loading the view, typically from a nib.
		square = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
		square.backgroundColor = UIColor.grayColor()
		view.addSubview(square)
		
		let barrier = UIView(frame: CGRect(x: 0, y: 300, width: 130, height: 20))
		barrier.backgroundColor = UIColor.redColor()
		view.addSubview(barrier)
		
		animator = UIDynamicAnimator(referenceView: view)
		gravity = UIGravityBehavior(items: [square])
		animator.addBehavior(gravity)
		
		collision = UICollisionBehavior(items: [square])
		collision.collisionDelegate = self
		// add a boundary that has the same frame as the barrier
		collision.addBoundaryWithIdentifier("barrier", forPath: UIBezierPath(rect: barrier.frame))
		collision.translatesReferenceBoundsIntoBoundary = true
		animator.addBehavior(collision)
		
//		collision.action = {
//			println("\(NSStringFromCGAffineTransform(square.transform)) \(NSStringFromCGPoint(square.center))")
//		}
		
		let itemBehaviour = UIDynamicItemBehavior(items: [square])
		itemBehaviour.elasticity = 0.6
//		itemBehaviour.allowsRotation = false
		animator.addBehavior(itemBehaviour)
		
		
		var updateCount = 0
		collision.action = {
			if updateCount % 3 == 0 {
				let outline = UIView(frame: self.square.bounds)
				outline.transform = self.square.transform
				outline.center = self.square.center
				
				outline.alpha = 0.5
				outline.backgroundColor = UIColor.clearColor()
				outline.layer.borderColor = self.square.layer.presentationLayer().backgroundColor
				outline.layer.borderWidth = 1.0
				self.view.addSubview(outline)
			}
			
			++updateCount
		}
	}
	
	func collisionBehavior(behavior: UICollisionBehavior, beganContactForItem item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying, atPoint p: CGPoint) {
//		println("Boundary contact occurred - \(identifier)")
		
		let collidingView = item as! UIView
		collidingView.backgroundColor = UIColor.yellowColor()
		UIView.animateWithDuration(0.3) {
			collidingView.backgroundColor = UIColor.grayColor()
		}
		
//		if !firstContact {
//			firstContact = true
//			let square = UIView(frame: CGRect(x: 30, y: 0, width: 100, height: 100))
//			square.backgroundColor = UIColor.grayColor()
//			view.addSubview(square)
//			
//			collision.addItem(square)
//			gravity.addItem(square)
//			
//			let attach = UIAttachmentBehavior(item: collidingView, attachedToItem: square)
//			animator.addBehavior(attach)
//		}
	}
	
	override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
		if snap != nil {
			animator.removeBehavior(snap)
		}
		
		if let touch = touches.first as? UITouch {
			snap = UISnapBehavior(item: square, snapToPoint: touch.locationInView(view))
			animator.addBehavior(snap)
		}
		
	}

	override func didReceiveMemoryWarning() {
		super.didReceiveMemoryWarning()
		// Dispose of any resources that can be recreated.
	}


}

